package com.helloandroid.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import com.helloandroid.R

// 固定分为4段,如果有需要再抽象一个通用的出来
data class PgData(
        var min: Float = 0f,
        var max: Float = 1f,
        var mid1: Float = 0.25f,
        var mid2: Float = 0.5f,
        var mid3: Float = 0.75f,
        var cur: Float = 0.5f,
        val texts: ArrayList<String> = arrayListOf("One", "Two", "Three", "Four")
) {
    val calProgress: Float
        get() {
            var percent = when (curIdx) {
                0 -> 0.25f * (cur - min) / (mid1 - min)
                1 -> 0.25f + 0.25f * (cur - mid1) / (mid2 - mid1)
                2 -> 0.5f + 0.25f * (cur - mid2) / (mid3 - mid2)
                else -> 0.75f + 0.25f * (cur - mid3) / (max - mid3)
            }
            if (percent < 0) {
                percent = 0f
            }
            if (percent > 1) {
                percent = 1f
            }
            return percent
        }

    val curIdx: Int
        get() {
            return when {
                cur <= mid1 -> 0
                cur <= mid2 -> 1
                cur <= mid3 -> 2
                else -> 3
            }
        }
}

class PgView @JvmOverloads constructor(
        ctx: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : View(ctx, attrs, defStyle) {

    companion object {
        val DefaultBgColor = Color.parseColor("#d3dae4")
        val DefaultFgColor = Color.parseColor("#2d7cfd")

        const val OverlapColor = Color.WHITE
        const val DefaultOffsetY = 20f

        // text color
        const val DefaultTextColor = Color.BLACK
        const val SelectedTextColor = Color.BLUE
    }

    var bgColor = 0
    var fgColor = 0

    var bgPaint: Paint
    var overlapPaint: Paint
    var textPaint: Paint
    var lineWidth = 0f

    var verGap: Float
    var pgData: PgData

    init {
        val typedArray = ctx.obtainStyledAttributes(attrs, R.styleable.PgView)
        bgColor = typedArray.getColor(R.styleable.PgView_bgColor, DefaultBgColor)
        fgColor = typedArray.getColor(R.styleable.PgView_fgColor, DefaultFgColor)
        verGap = typedArray.getDimension(R.styleable.PgView_textOffsetY, DefaultOffsetY)
        typedArray.recycle()

        // init paint
        bgPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        bgPaint.color = bgColor
        bgPaint.style = Paint.Style.STROKE
        bgPaint.strokeCap = Paint.Cap.ROUND
        lineWidth = resources.getDimension(R.dimen.dp8)
        bgPaint.strokeWidth = lineWidth

        //
        overlapPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        overlapPaint.color = OverlapColor
        overlapPaint.strokeWidth = resources.getDimension(R.dimen.dp2)

        //
        textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        textPaint.textSize = resources.getDimension(R.dimen.sp16)

        pgData = PgData()
    }

    fun updatePgData(data: PgData) {
        if (pgData != data) {
            pgData = data
            invalidate()
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val ctx = canvas!!

        val initY = paddingTop + lineWidth / 2

        // 绘制一个背景
        val sx = (paddingLeft + marginLeft).toFloat()
        val ex = (width - paddingRight - marginRight).toFloat()
        bgPaint.color = bgColor
        ctx.drawLine(sx, initY, ex, initY, bgPaint)

        val len = ex - sx

        // 根据进度 绘制前景
        val fgEndX = sx + len * pgData.calProgress
        bgPaint.color = fgColor
        ctx.drawLine(sx, initY, fgEndX, initY, bgPaint)

        // 进行4等分,绘制3个竖着的东西
        val step = len / 4f
        bgPaint.color = OverlapColor
        for (i in 0..2) {
            val tx = sx + (i + 1) * step
            ctx.drawLine(tx, initY - lineWidth / 2f + 1, tx, initY + lineWidth / 2f - 1, overlapPaint)
        }

        // 绘制文字
        val curIdx = pgData.curIdx
        for (item in pgData.texts.withIndex()) {
            val midx = sx + step / 2f + step * item.index
            val midy = initY + textPaint.descent() + verGap

            // 计算文字有多宽
            val textWidth = textPaint.measureText(item.value)

            if (curIdx == item.index) {
                textPaint.color = SelectedTextColor
            } else {
                textPaint.color = DefaultTextColor
            }
            canvas.drawText(item.value, midx - textWidth / 2f, midy, textPaint)
        }
    }

}